<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../include/header.jsp" %>   
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Member List</title>
<script type="text/javascript">
	$(document).ready(function(){		
		$("#option_date").val(new Date().toDateInputValue());		
		memberList();
	});	
	
	function memberList(){
		var keyword = $("#keyword").val();
		var option_level = $("#option_level").val();
		var option_rate = $("#option_rate").val();
		var option_limit = $("#option_limit").val();
		var option_order = $("#option_order").val();
		var option_date =  $("#option_date").val();
		var offset = $("#offset").val();
		if( offset == "" ){
			offset  = 0;
		}
		var param = "option_level="+option_level
			+"&option_rate="+option_rate
			+"&option_limit="+option_limit
			+"&option_order="+option_order
			+"&option_date="+option_date
			+"&keyword="+keyword
			+"&offset="+offset;
		$.ajax({
			type:"post",
			url:"${path}/member/memberList.do",
			data:param,
			success:function(msg){			
				$("#div_member_list").html(msg);	
			}		
		});
	}
	
	function nextPage(){
		var option_limit = $("#option_limit").val();
		var offset = $("#offset").val();
		var count = $("#count").val();
		if( offset == "" ){
			offset  = 0;
		}
		if((parseInt(count)-parseInt(offset)) <= parseInt(option_limit)){
			alert("마지막페이지 입니다");
			return;
		}		
		$("#offset").val(parseInt(option_limit)+parseInt(offset));		
		memberList();
	}
	function prePage(){		
		var option_limit = $("#option_limit").val();
		var offset = $("#offset").val();
		if(offset== 0){
			alert("첫페이지입니다");
			return;
		}
		$("#offset").val(parseInt(offset)-parseInt(option_limit));		
		memberList();
	}
	
	Date.prototype.toDateInputValue = (function() {
	    var local = new Date(this);
	    local.setMinutes(this.getMinutes() - this.getTimezoneOffset() -43200);
	    return local.toJSON().slice(0,10);
	});
</script>
<!-- 관리자 일 경우 -->
<c:if test="${sessionScope.level == 0 }">
<script type="text/javascript">
$(document).ready(function(){		
	$("#condition_date").val(new Date().toDateInputValue());	
});	

function update_level(userid){	
	if(confirm(userid+"회원의 정보를 수정하시겠습니까?")){
		var select_value = $("#select_"+userid).val();
		var param = "userid="+userid+"&level="+select_value;
		$.ajax({
			type:"post",
			url:"${path}/admin/mb_level_update.do",
			data:param,
			success:function(){
				memberList();
				alert("회원정보가 수정되었습니다");
			}		
		});		
	}
}
function autoLevelManage(){
	if(confirm("전체 회원을 등급조정을 하시겠습니까?")){
		var cd_rate_sum1 = $("#condition_rate_sum1").val();
		var cd_rate_sum2 = $("#condition_rate_sum2").val();
		var cd_rate_sum3 = $("#condition_rate_sum3").val();
		var cd_rate_sum4 = $("#condition_rate_sum4").val();
		var cd_rate_avg1 = $("#condition_rate_avg1").val();
		var cd_rate_avg2 = $("#condition_rate_avg2").val();
		var cd_rate_avg3 = $("#condition_rate_avg3").val();		
		var cd_rate_avg4 = $("#condition_rate_avg4").val();
		var cd_date = $("#condition_date").val();
		var param = "cd_rate_sum1="+cd_rate_sum1
		+"&cd_rate_sum2="+cd_rate_sum2
		+"&cd_rate_sum3="+cd_rate_sum3
		+"&cd_rate_sum4="+cd_rate_sum4
		+"&cd_rate_avg1="+cd_rate_avg1
		+"&cd_rate_avg2="+cd_rate_avg2
		+"&cd_rate_avg3="+cd_rate_avg3
		+"&cd_rate_avg4="+cd_rate_avg4
		+"&cd_date="+cd_date;
		$.ajax({
			type:"post",
			url:"${path}/admin/auto_level_update.do",
			data:param,
			success:function(){
				memberList();
				alert("전체회원의 레벨이 조정되었습니다");
			}
		});		
	}	
}

</script>
</c:if>
</head>
<body>
	<table border="1" style="border:solid black; width: 700px; height: 130px; margin: 10 auto;">
		<tr>
			<th>Level</th>
			<th>기준</th>
			<th>결과제한</th>
			<th>정렬</th>
			<th>기간</th>		
		</tr>
		<tr>
			<th>
				<select id="option_level" name="option_level" onchange="javascript:memberList()">
					<option value="0">All</option>
					<option value="1">마스터쉐프</option>
					<option value="2">쉐프</option>
					<option value="3">선임요리사</option>
					<option value="4">견습요리사</option>
					<option value="5">주방보조</option>
				</select>
			</th>
			<th>
				<select id="option_rate" name="option_rate"   onchange="javascript:memberList()">					
					<option value="rate_sum">평점합계</option>
					<option value="rate_avg">평점평균</option>
					<option value="level">등급</option>
					<option value="count">레시피등록수</option>					
				</select>
			</th>
			<th>
				<select id="option_limit" name="option_limit"  onchange="javascript:memberList()">					
					<option value="30">30</option>
					<option value="50">50</option>					
					<option value="100">100</option>					
				</select>
				개
			</th>
			<th>
				<select id="option_order" name="option_order"  onchange="javascript:memberList()">					
					<option value="desc">내림차순</option>					
					<option value="asc">오름차순</option>
				</select>
			</th>
			<th>
				<input type="date" id="option_date" name="option_date" onchange="javascript:memberList()">
				부터<br>(기본값:30일전 기준)
			</th>

		</tr>
		<tr>
			<th colspan="5">
				아이디 또는 이름
				<input type="text" id="keyword" name="keyword" size="10" >				
				<input type="button" value="검색" onclick="javascript:memberList()">				
			</th>
		</tr>
	</table>
	<!--  관리자  메뉴 -->
	<c:if test="${sessionScope.level == 0 }">
		<table border="1" style="border:solid black; width: 700px; height: 175px; margin: 10 auto;">
			<tr>
				<th rowspan="2">등 급</th>
				<th colspan="2">조 건</th>
				<th rowspan="2">비 고</th>
			</tr>
			<tr>
				<th>평점합계 점수</th>
				<th>평점평균 상위</th>				
			</tr>
			<tr>
				<th>마스터쉐프</th>
				<th>
					<input type="text" size="5" id="condition_rate_sum1" name="condition_rate_sum1" value="150">개 이상
				</th>
				<th>
					<input type="text" size="5"  id="condition_rate_avg1" name="condition_rate_avg1" value="10">%
				</th>
				<th rowspan="2">
					선택일자 ~ 현재<br>
					<input type="date" id="condition_date" name="condition_date">
				</th>
			</tr>
			<tr>
				<th>쉐프</th>
				<th>
					<input type="text" size="5"  id="condition_rate_sum2" name="condition_rate_sum2" value="150">개 이상
				</th>
				<th>
					<input type="text" size="5"  id="condition_rate_avg2" name="condition_rate_avg2" value="30">%
				</th>
			</tr>
			<tr>
				<th>선임요리사</th>
				<th>
					<input type="text" size="5"  id="condition_rate_sum3" name="condition_rate_sum3" value="90">개 이상
				</th>
				<th>
					<input type="text" size="5"  id="condition_rate_avg3" name="condition_rate_avg3" value="70">%
				</th>
				<th rowspan="2">
					<input type="button" value="자동등급조절" id="btnLevelSet" name="btnLevelSet" onclick="autoLevelManage()"
					style="font-weight: bold; height: 30px; width: 150px;"/>			
				</th>
			</tr>
				<tr>
				<th>견습요리사</th>
				<th>
					<input type="text" size="5"  id="condition_rate_sum4" name="condition_rate_sum4" value="30">개 이상
				</th>
				<th>
					<input type="text" size="5"  id="condition_rate_avg4" name="condition_rate_avg4" value="100">%
				</th>
			</tr>
		</table>	
	</c:if>
	<hr style="border: solid black;">
	
	<div id="div_member_list"></div>
	<div style="margin: 5 auto; width: 40%;">		
		<input type="button"	id="btn_pre" name="btn_pre" value="이전페이지" onclick="prePage()">
		<input type="button"	id="btn_next" name="btn_next" value="다음페이지" onclick="nextPage()">
		<input type="hidden" id="offset" name="offset" >
	</div>
</body>
</html>